package remote

import (
	"log"
	"net"

	D "gitee.com/dark.H/go-remote-repl/datas"
)

var (
	DefaultHandleRecvJson func(data D.Gi)
	DefaultRidEFunc       func(con net.Conn)
	DefaultGetAPINAMEs    func() []string
	DefaultLPC            func(name string, lpc func(label string, opts D.G) (string, net.Conn, error))
)

func (man *ApiServerMan) FunctionDo() {
	ops := []string{}
	ops = append(ops, DefaultGetAPINAMEs()...)
	ops = append(ops, "exit")
	name, err := man.Choose("api funcitons", ops...)
	co := man.con
	if name == "exit" {
		return
	}

	if err != nil {
		log.Println(err)
		co.Close()
		return
	}
	if DefaultLPC != nil {
		DefaultLPC(name, func(label string, opts D.G) (string, net.Conn, error) {
			r, err := man.InputJson(label, opts)
			return r, man.con, err
		})
	} else {
		log.Println("no LPC can be called!")
	}
}
